"""
这是一段示例代码。
This is an example code.
by Yin.
"""
import sys
sys.path.append("..")
import numpy as np
from tensor import Tensor,zeroGrad

#初始化参数
x = Tensor((400,1))
y = Tensor((400,1))
w = Tensor((1,1),way='Adam')
b = Tensor((1,1),way='Adam')
w.inputValue(np.array([[-0.2]]))
b.zeros()

#伪造数据集
x_array = np.arange(-2,2,0.01)[:,np.newaxis]
y_array = x_array*0.2+0.5+(np.random.normal(0,0.5,400)/10)[:,np.newaxis]
x.inputValue(x_array)
y.inputValue(y_array)

#搭建前向传播路径 
hiddenLayer = x.matmul(w)
out = hiddenLayer+b
loss = out.MSELoss(y)

#设置超参数
lr = 0.1
b1 = 0.4
b2 = 0.2

#开始训练
for i in range(10001):#
	zeroGrad(out,w,b,hiddenLayer)#梯度清零
	x.forward()#前向传播
	loss.backward()#反向传播求梯度
	#更新参数
	w.step(lr,b1=b1,b2=b2)
	b.step(lr,b1=b1,b2=b2)
	if i%1000==0:
		#打印loss和w，b的值
		print('第',i,'轮：')
		loss.show_mean()
		print('w:\n',w,'\nb:\n',b)
		print('\n\n\n')
